<!---->
<template>
    <div class="page_optionDetail">
        <common-header :title="'基本信息编辑'">
            <span slot="overwrite-left"></span>
        </common-header>
        <div class="btn-save" @click="saveInfo">保存</div>
        <div class="pages-main" style="height: calc( 100vh - 45px)">
            <div class="item-info">
                <div class="item-item">
                    <x-input title="税务登记号" v-model="enterInfo.taxpayercode" placeholder="请填写税务登记号"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="生产厂址" v-model="enterInfo.productsite" placeholder="请填写生产厂址"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="邮政编码" v-model="enterInfo.postalcode" placeholder="请填写邮政编码"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="传真号码" v-model="enterInfo.faxnumber" placeholder="请填写传真号码"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="企业网址" v-model="enterInfo.enterpriseurl" placeholder="请填写企业网址"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="企业邮箱" v-model="enterInfo.enterpriseemail" placeholder="请填写企业邮箱"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="员工总人数" v-model="enterInfo.totalstaff" type="number"
                             placeholder="请填写员工总人数"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="本地员工人数" v-model="enterInfo.localstaff" type="number"
                             placeholder="请填写本地员工人数"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="资金补贴（万元）" type="number" v-model="enterInfo.fundsubsidy"
                             placeholder="请填写资金补贴"></x-input>
                </div>
            </div>
            <div class="item-info">
                <div class="item-item">
                    <x-input title="补贴已使用（万元" type="number" v-model="enterInfo.usefundsubsidy"
                             placeholder="请填写补贴已使用"></x-input>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue'
    import {XInput, Selector, Datetime} from 'vux'
    import {checkIntNumber} from '../../utils/validate'
    import company from '../../services/company'
    import {
        checkPostalcode,
        checkProductsite,
        checkFaxnumber,
        checkEmail,
        checkEnterpriseurl,
        checkTaxpayercode
    } from '../../utils/validate'

    export default {
        name: "optionDetail",
        components: {
            XInput,
            Selector,
            Datetime
        },
        data() {
            return {
                id: this.$route.query.id,
                typecodeList: [],
                enterInfo: {
                    name: '',
                    registeredcapital: '',
                    criditecode: '',
                    contactuser: '',
                    legalidnumber: '',
                    contactphone: '',
                    lat: '',
                    lon: '',
                    location: '',
                    establishtime: '',
                    categoryText: '',
                    maincamp: '',
                    products: [],
                    taxpayercode: '',
                    productsite: '',
                    postalcode: '',
                    faxnumber: '',
                    enterpriseurl: '',
                    enterpriseemail: '',
                    totalstaff: '',
                    localstaff: '',
                    fundsubsidy: '',
                    usefundsubsidy: '',
                    legalname: '',
                    picurl: ''
                }
            }
        },

        created() {

        },
        mounted() {
            this.getDetail()
            this.optionbytypecode()
        },
        methods: {
            // 获取企业详情
            getDetail() {
                let _param = this.$tools.invokeUAF('enterprise/detail', {
                    id: this.id,
                })
                company.getEnterpriseDetail(_param).then(res => {
                    let data = res.data.data;
                    this.enterInfo = data;
                    this.enterInfo.postalcode = data.postalcode === 0 ? '' : data.postalcode;
                    this.enterInfo.totalstaff = data.totalstaff === 0 ? '' : data.totalstaff;
                    this.enterInfo.localstaff = data.localstaff === 0 ? '' : data.localstaff;
                    this.enterInfo.fundsubsidy = data.fundsubsidy === 0 ? '' : data.fundsubsidy;
                    this.enterInfo.usefundsubsidy = data.usefundsubsidy === 0 ? '' : data.usefundsubsidy;

                })
            },

            // 获取产品单位
            optionbytypecode() {
                let _param = this.$tools.invokeUAF('sys/optionbytypecode', {
                    typecode: 'UNIT_TYPE',
                })
                company.optionbytypecode(_param).then(res => {
                    let data = res.data.data;
                    // this.enterInfo = data;
                    this.typecodeList = data;
                })
            },

            // 保存企业信息
            saveInfo() {

                if (this.enterInfo.taxpayercode) {
                    if (!checkTaxpayercode(this.enterInfo.taxpayercode)) {
                        return;
                    }
                }

                if (this.enterInfo.productsite) {
                    if (!checkProductsite(this.enterInfo.productsite)) {
                        return;
                    }
                }

                if (this.enterInfo.postalcode) {
                    if (!checkPostalcode(this.enterInfo.postalcode)) {
                        return;
                    }
                }

                if (this.enterInfo.faxnumber) {
                    if (!checkFaxnumber(this.enterInfo.faxnumber)) {
                        return;
                    }
                }

                if (this.enterInfo.enterpriseurl) {
                    if (!checkEnterpriseurl(this.enterInfo.enterpriseurl)) {
                        return;
                    }
                }

                if (this.enterInfo.enterpriseemail) {
                    if (!checkEmail(this.enterInfo.enterpriseemail)) {
                        return;
                    }
                }

                if (this.enterInfo.totalstaff) {
                    if (!checkIntNumber(this.enterInfo.totalstaff, '员工总人数')) {
                        return;
                    }
                }

                if (this.enterInfo.localstaff) {
                    if (!checkIntNumber(this.enterInfo.localstaff, '本地员工数')) {
                        return;
                    }
                    if (this.enterInfo.localstaff > this.enterInfo.totalstaff) {
                        Vue.$vux.toast.show({
                            text: '本地员工数不能大于员工总人数',
                            type: 'text',
                            position: 'middle',
                        })
                        return;
                    }
                }

                if (this.enterInfo.fundsubsidy) {
                    if (!checkIntNumber(this.enterInfo.fundsubsidy, '资金已补贴')) {
                        return;
                    }
                }

                if (this.enterInfo.usefundsubsidy) {
                    if (!checkIntNumber(this.enterInfo.usefundsubsidy, '补贴已使用')) {
                        return;
                    }

                    if (this.enterInfo.usefundsubsidy > this.enterInfo.fundsubsidy) {
                        Vue.$vux.toast.show({
                            text: '补贴已使用不能大于资金已补贴',
                            type: 'text',
                            position: 'middle',
                        })
                        return;
                    }
                }
                let url = '';
                if (this.enterInfo.picurl.length) {
                    this.enterInfo.picurl.forEach(item => {
                        url = url ? url + ',' + item.fileurl : item.fileurl
                    })
                }
                this.enterInfo.picurl = url;
                let _param = this.$tools.invokeUAF('enterprise/getEnterpriseModify', {})
                company.getEnterpriseModify(_param, this.enterInfo).then(res => {
                })
            },
        },
    };
</script>

<style lang="less" rel="stylesheet/less" type="text/less">
    .page_optionDetail {
        .weui-label {
            width: 140px !important;
        }


    }
</style>
